package com.juhuacha.tryrxjava.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.juhuacha.tryrxjava.R;

/**
 * Created by Administrator on 2016/9/2.
 * 邮箱       juhuacha18@gmail.com
 * E-Mail     juhuacha18@gmail.com
 * CopyRight  9hgame.com reserved
 */
public class CollapsibleTextView extends LinearLayout implements
        View.OnClickListener {

    /**
     * default text show max lines
     */
    private static final int DEFAULT_MAX_LINE_COUNT = 3;

    public static final int COLLAPSIBLE_STATE_NONE = 0;
    public static final int COLLAPSIBLE_STATE_SHRINKUP = 1;
    public static final int COLLAPSIBLE_STATE_SPREAD = 2;

    private CollapsStateCallBack mCallback;

    private TextView desc;
    private TextView descOp;

    private String shrinkup;
    private String spread;
    private int mState;
    private boolean flag;

    public CollapsibleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        shrinkup = context.getString(R.string.desc_shrinkup);
        spread = context.getString(R.string.desc_spread);
        View view = inflate(context, R.layout.collapsible_textview, this);
        view.setPadding(0, -1, 0, 0);
        desc = (TextView) view.findViewById(R.id.desc_tv);
        descOp = (TextView) view.findViewById(R.id.desc_op_tv);
        descOp.setOnClickListener(this);
    }

    public void setmCallback(CollapsStateCallBack mCallback) {
        this.mCallback = mCallback;
    }

    public CollapsibleTextView(Context context) {
        this(context, null);
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void setmState(int mState) {
        this.mState = mState;
    }

    public final void setDesc(CharSequence charSequence, TextView.BufferType bufferType) {
        desc.setText(charSequence, bufferType);
        mState = COLLAPSIBLE_STATE_SPREAD;
        requestLayout();
    }

    public final void setDesc(CharSequence charSequence, TextView.BufferType bufferType, int state) {
        desc.setText(charSequence, bufferType);
        mState = state;
        requestLayout();
    }

    @Override
    public void onClick(View v) {
        flag = false;
        requestLayout();

        if (mCallback != null) {
            if (mState == COLLAPSIBLE_STATE_SHRINKUP) {
                mCallback.stateShrinkUp();
            }else if(mState == COLLAPSIBLE_STATE_SPREAD){
                mCallback.stateSpread();
            }else{
                mCallback.stateNone();
            }

        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!flag) {
            flag = true;
            if (desc.getLineCount() <= DEFAULT_MAX_LINE_COUNT) {
                mState = COLLAPSIBLE_STATE_NONE;
                descOp.setVisibility(View.GONE);
                desc.setMaxLines(DEFAULT_MAX_LINE_COUNT + 1);
            } else {
                post(new InnerRunnable());
            }
        }
    }

    class InnerRunnable implements Runnable {
        @Override
        public void run() {
            if (mState == COLLAPSIBLE_STATE_SPREAD) {
                desc.setMaxLines(DEFAULT_MAX_LINE_COUNT);
                descOp.setVisibility(View.VISIBLE);
                descOp.setText(spread);
                mState = COLLAPSIBLE_STATE_SHRINKUP;

            } else if (mState == COLLAPSIBLE_STATE_SHRINKUP) {
                desc.setMaxLines(Integer.MAX_VALUE);
                descOp.setVisibility(View.VISIBLE);
                descOp.setText(shrinkup);
                mState = COLLAPSIBLE_STATE_SPREAD;
            }
        }
    }

    public interface CollapsStateCallBack {
        void stateSpread();

        void stateShrinkUp();

        void stateNone();
    }
}
